package com.zhijian.wharf.controller.web;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.page.Page;
import com.zhijian.wharf.entity.vo.request.MqttTopicSaveOrUpdateVo;
import com.zhijian.wharf.entity.vo.request.MqttTopicSearchVo;
import com.zhijian.wharf.entity.vo.response.MqttTopicVo;
import com.zhijian.wharf.holder.CurrentWebUserHolder;
import com.zhijian.wharf.service.MqttTopicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 消息主题 控制层
 *
 * @author auto
 * @version 1.0
 * @date 2024-08-12 19:28:30
 */
@Slf4j
@Controller
@Api(tags = "消息主题")
public class MqttTopicController {

    @Autowired
    private MqttTopicService service;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/mqtt/topic")
    @ApiOperation("新增")
    public boolean insert(@RequestBody MqttTopicSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        param.setCreateUserName(CurrentWebUserHolder.getUser().getName());
        param.setCreateUserId(CurrentWebUserHolder.getUser().getId());
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/mqtt/topic/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/mqtt/topic/{id}")
    @ApiOperation("根据id查询")
    public MqttTopicVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/mqtt/topic/page")
    @ApiOperation("分页查询")
    public Page<MqttTopicVo> selectByCondition(@RequestBody(required = false) MqttTopicSearchVo param) {
        if (param == null)
            param = MqttTopicSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 发布
     *
     * @param id
     * @return
     */
    @Response
    @PutMapping("/mqtt/topic/{id}")
    @ApiOperation("发布")
    public boolean send(@PathVariable("id") Long id) {
        return service.publish(id);
    }

    /**
     * 订阅
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/mqtt/topic/sub/{id}")
    @ApiOperation("订阅")
    public boolean sub(@PathVariable("id") Long id) {
        return service.sub(id, CurrentWebUserHolder.getUser().getId());
    }

}